object chaoshi {
}
import java.io.{FileInputStream, FileOutputStream, ObjectInputStream, ObjectOutputStream}
import scala.collection.mutable.ListBuffer

// 定义会员类
case class Member(cardNo: Int, name: String, password: String, var score: Int, registerTime: String) extends Serializable

// 定义超市类
class SuperMarket(val name: String, val address: String, val phone: String) extends Serializable {
  private var members: ListBuffer[Member] = ListBuffer()  // 用于存储会员信息

  // 功能1：开卡
  def addMember(cardNo: Int, name: String, password: String): Unit = {
    if (password.length < 6) {
      println("密码至少6位！")
    } else if (members.exists(_.cardNo == cardNo)) {
      println("卡号已存在，请重新输入！")
    } else {
      val registerTime = java.time.LocalDateTime.now().toString
      members += Member(cardNo, name, password, 0, registerTime)
      println(s"会员卡号${cardNo}的会员添加成功！")  // 使用 ${} 包裹变量
    }
  }

  // 功能2：积分累计
  def increaseScore(cardNo: Int, password: String, amount: Double): Unit = {
    val member = members.find(m => m.cardNo == cardNo && m.password == password)
    member match {
      case Some(m) =>
        val score = amount.toInt
        m.score += score
        println(s"会员卡号${m.cardNo}的积分增加了${score}分，当前积分${m.score}分")  // 使用 ${} 包裹变量
      case None =>
        println("卡号或密码错误")
    }
  }

  // 功能3：查询积分
  def queryScore(cardNo: Int, password: String): Unit = {
    val member = members.find(m => m.cardNo == cardNo && m.password == password)
    member match {
      case Some(m) =>
        println(s"会员卡号${m.cardNo}的当前积分${m.score}分")  // 使用 ${} 包裹变量
      case None =>
        println("卡号或密码错误")
    }
  }

  // 功能4：积分兑换
  def exchange(cardNo: Int, password: String, score: Int): Unit = {
    val member = members.find(m => m.cardNo == cardNo && m.password == password)
    member match {
      case Some(m) =>
        if (m.score >= score) {
          m.score -= score
          println(s"会员卡号${m.cardNo}的积分兑换成功，当前积分${m.score}分")  // 使用 ${} 包裹变量
        } else {
          println("积分不足")
        }
      case None =>
        println("卡号或密码错误")
    }
  }

  // 功能5：退出系统（保存数据）
  def save(): Unit = {
    val fos = new FileOutputStream("supermarket.obj")
    val oos = new ObjectOutputStream(fos)
    oos.writeObject(this)
    oos.close()
    fos.close()
  }

  // 加载数据
  def load(): Unit = {
    val fis = new FileInputStream("supermarket.obj")
    val ois = new ObjectInputStream(fis)
    val loadedSupermarket = ois.readObject().asInstanceOf[SuperMarket]
    members = loadedSupermarket.members
    ois.close()
    fis.close()
  }
}

// 主程序
object SuperMarket {
  def main(args: Array[String]): Unit = {
    var supermarket: SuperMarket = null
    val file = new java.io.File("supermarket.obj")

    // 检查文件是否存在，加载或创建超市对象
    if (file.exists()) {
      supermarket = load()
    } else {
      supermarket = new SuperMarket("天天购物", "天天购物地址", "1234567890")
    }

    println(s"欢迎来到${supermarket.name}超市！")
    while (true) {
      println("\n请选择功能：")
      println("1. 开卡")
      println("2. 积分累计")
      println("3. 查询积分")
      println("4. 积分兑换")
      println("5. 退出")
      val choice = scala.io.StdIn.readInt()

      choice match {
        case 1 =>
          println("请输入会员卡号：")
          val cardNo = scala.io.StdIn.readInt()
          println("请输入会员姓名：")
          val name = scala.io.StdIn.readLine()
          println("请输入会员密码：")
          val password = scala.io.StdIn.readLine()
          supermarket.addMember(cardNo, name, password)
        case 2 =>
          println("请输入会员卡号：")
          val cardNo = scala.io.StdIn.readInt()
          println("请输入会员密码：")
          val password = scala.io.StdIn.readLine()
          println("请输入消费金额：")
          val amount = scala.io.StdIn.readDouble()
          supermarket.increaseScore(cardNo, password, amount)
        case 3 =>
          println("请输入会员卡号：")
          val cardNo = scala.io.StdIn.readInt()
          println("请输入会员密码：")
          val password = scala.io.StdIn.readLine()
          supermarket.queryScore(cardNo, password)
        case 4 =>
          println("请输入会员卡号：")
          val cardNo = scala.io.StdIn.readInt()
          println("请输入会员密码：")
          val password = scala.io.StdIn.readLine()
          println("请输入兑换积分：")
          val score = scala.io.StdIn.readInt()
          supermarket.exchange(cardNo, password, score)
        case 5 =>
          supermarket.save()
          println("退出系统，感谢使用！")
          return
        case _ =>
          println("无效的选择，请重新输入")
      }
    }
  }

  // 加载超市对象
  def load(): SuperMarket = {
    val ois = new ObjectInputStream(new FileInputStream("supermarket.obj"))
    val sp = ois.readObject().asInstanceOf[SuperMarket]
    ois.close()
    sp
  }
}